from django.db import models

# Create your models here.
class Route(models.Model):
    name = models.CharField(null=False, max_length=32, verbose_name="线路名称", help_text="线路名称")
    station = models.ManyToManyField(to="Station",null=True, verbose_name="停靠站点", db_constraint=False, help_text="停靠站点")

    class Meta:
        app_label = 'mybus'
        db_table = "mybus_routes"
        verbose_name = "路线表"
        verbose_name_plural = verbose_name
        ordering = ("id",)

class Station(models.Model):
    name = models.CharField(null=False, max_length=32, verbose_name="站点名称", help_text="站点名称")
    station_time = models.CharField(null=False, max_length=16, verbose_name="站点时间", help_text="站点时间") # 站点时间
    station_order = models.IntegerField(default=1, verbose_name="站点顺序", help_text="站点顺序") # 站点顺序

    class Meta:
        app_label = 'mybus'
        db_table = "mybus_stations"
        verbose_name = "站点表"
        verbose_name_plural = verbose_name
        ordering = ("station_order",)

class Bus(models.Model):
    name = models.CharField(null=False, max_length=32, verbose_name="车牌号码", help_text="车牌号码")

    class Meta:
        app_label = 'mybus'
        db_table = "mybus_bus"
        verbose_name = "车辆表"
        verbose_name_plural = verbose_name
        ordering = ("id",)

class Position(models.Model):
    report_time = models.DateTimeField() # 报告时间

    vehicle = models.ForeignKey(
        to="Bus",
        verbose_name="车辆",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="车辆",
    )# 车辆

    route = models.ForeignKey(
        to="Route",
        verbose_name="路线",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="路线",
    )# 路线

    station = models.ForeignKey(
        to="Station",
        verbose_name="站点",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="站点",
    )# 路线

    class Meta:
        app_label = 'mybus'
        db_table = "mybus_position"
        verbose_name = "报站表"
        verbose_name_plural = verbose_name
        ordering = ("-report_time",)

class Dispatch(models.Model):
    driver = models.ForeignKey(
        to="system.Users",
        verbose_name="司机",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="司机",
    )# 司机

    vehicle = models.ForeignKey(
        to="Bus",
        verbose_name="车辆",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="车辆",
    )# 车辆

    route = models.ForeignKey(
        to="Route",
        verbose_name="路线",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="路线",
    )# 路线

    edate = models.DateField()# 排班日期

    class Meta:
        app_label = 'mybus'
        db_table = "mybus_dispatchs"
        verbose_name = "排班表"
        verbose_name_plural = verbose_name
        ordering = ("-edate",)